<?php
/*
 * 用户控制器
 */
namespace app\admin\controller;
use think\Db;
use think\Config;
class Member extends Comm
{
	public function index(){
		$prefix = Config('database.prefix');
		$field= input('get.field');
		$keyword=input('get.keyword');
		$orderd=input('get.order');
		$where='a.uid <> 3';

        if ($orderd == 'asc') {
            $order = "a.t asc";
        } elseif (($orderd == 'desc')) {
            $order = "a.t desc";
        } else {
            $order = "a.uid asc";
        }
        if ($keyword <> '') {
            if ($field == 'user') {
                $where = "a.user LIKE '%$keyword%'";
            }
            if ($field == 'phone') {
                $where = "a.phone LIKE '%$keyword%'";
            }
            if ($field == 'qq') {
                $where = "a.qq LIKE '%$keyword%'";
            }
            if ($field == 'email') {
                $where = "a.email LIKE '%$keyword%'";
            }
        }
		$pagesize = 20;
		$count = Db::name('member')->where('uid','neq',3)->count();		
		$list=Db::name('member')->alias('a')
		->field(['a.user','a.head','a.sex','a.birthday','a.phone','a.qq','a.email','a.uid','c.id'=>'gid','c.title'])
		->join("auth_group_access b",'a.uid=b.uid')
		->join("auth_group c",'c.id=b.group_id')
		->where($where)->where('a.uid','neq',3)->order($order)->paginate($pagesize,$count,['query' => array('field'=>$field,'keyword'=>$keyword,'order'=>$orderd)]);
		//echo db('member')->getLastsql();die;
		$page = $list->render();
		$this->assign('list',$list);
		$group = Db::name('auth_group')->field('id,title')->select();
        $this->assign('group', $group);
		return $this->fetch();
	}
	//新增
	public function add()
    {

        $usergroup = Db::name('auth_group')->field('id,title')->select();
        $this->assign('usergroup', $usergroup);
        return $this->fetch('form');
    }
    //编辑
   public function edit()
    {
		$uid = input('param.uid',false, 'strip_tags');
        if ($uid) {
            //$member = M('member')->where("uid='$uid'")->find();
            $prefix = config('database.prefix');
            $member = Db::table("{$prefix}member")
			->field('')
			->join("{$prefix}auth_group_access" , "{$prefix}member.uid = {$prefix}auth_group_access.uid")->where("{$prefix}member.uid=$uid")->find();

        } else {
            $this->error('参数错误！');
        }

        $usergroup = Db::name('auth_group')->field('id,title')->select();
        $this->assign('usergroup', $usergroup);

        $this->assign('member', $member);
        return $this->fetch('form');
    }
    //新增提交
	public function update($ajax = '')
    {
        if ($ajax == 'yes') {
            $uid = input('get.uid', 0, 'intval');
            $gid = input('get.gid', 0, 'intval');
            Db::name('auth_group_access')->where("uid='$uid'")->update(array('group_id' => $gid));
            return json(array('code' => 200, 'msg' => '换用户组成功'));	
        }

        $uid = isset($_POST['uid']) ? intval($_POST['uid']) : false;
        $user = isset($_POST['user']) ? htmlspecialchars($_POST['user'], ENT_QUOTES) : '';
        $group_id = isset($_POST['group_id']) ? intval($_POST['group_id']) : 0;
        if (!$group_id) {
            $this->error('请选择用户组！');
        }
        $password = isset($_POST['password']) ? trim($_POST['password']) : false;
        if ($password) {
            $data['password'] = password($password);
        }
        $head = input('post.head', '', 'strip_tags');
        $data['sex'] = isset($_POST['sex']) ? intval($_POST['sex']) : 0;
        $data['head'] = $head ? $head : '';
        $data['birthday'] = isset($_POST['birthday']) ? strtotime($_POST['birthday']) : 0;
        $data['phone'] = isset($_POST['phone']) ? trim($_POST['phone']) : '';
        $data['qq'] = isset($_POST['qq']) ? trim($_POST['qq']) : '';
        $data['email'] = isset($_POST['email']) ? trim($_POST['email']) : '';
        $data['t'] = time();
        if (!$uid) {
            if ($user == '') {
                $this->error('用户名称不能为空！');
            }
            if (!$password) {
                $this->error('用户密码不能为空！');
            }
            if (Db::name('member')->where("user='$user}'")->count()) {
                $this->error('用户名已被占用！');
            }
            $data['user'] = $user;
            $uid = Db::name('member')->insertGetId($data);
            Db::name('auth_group_access')->insert(array('group_id' => $group_id, 'uid' => $uid));
            addlog('新增会员，会员UID：' . $uid);
        } else {
            Db::name('auth_group_access')->where("uid=$uid")->update(array('group_id' => $group_id));
            addlog('编辑会员信息，会员UID：' . $uid);
            Db::name('member')->where("uid=$uid")->update($data);

        }
        //$this->success('操作成功！');
		return json(array('code' => 200, 'msg' => '操作成功'));
    }
	
	
    public function del()
    {
        $ids = input('param.');
        if ($ids) {
            if (is_array($ids)) {
				if (request()->isGet()){
					$ids = implode(',', $ids);
				}else{
					$ids = implode(',', $ids['ids']);	
				}
                $map['uid'] = array('in', $ids);
            } else {
                $map = 'uid=' . $uids;
            }
            if (db('member')->where($map)->delete()) {
				db('auth_group_access')->where($map)->delete();
                addlog('删除账号，ID：' . $uids);
				return json(array('code' => 200, 'msg' => '恭喜，删除用户成功'));	
                //$this->success('恭喜，删除成功！');
            } else {
                $this->error('参数错误！');
            }
        } else {
            $this->error('参数错误！');
        }
    }	
	
	
	
	
	
	
	
}
?>